package com.yonyou.jrmob.contacts.contact.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yonyou.jrmob.contacts.R;
import com.yonyou.jrmob.contacts.contact.bean.InterfacePeopleVO_ct;
import com.yonyou.jrmob.contacts.contact.utils.CSIMCardUtil_ct;
import com.yonyou.jrmob.contacts.contact.view.dialog.MaterialDialog;

import java.util.ArrayList;
import java.util.List;

import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.CommonUtil;

/**
 * Created by GaoHW on 2016/7/13.
 */
public class InterfacePropleAdapter_ct extends BaseAdapter {

    private List<InterfacePeopleVO_ct> data;
    private Context context;
    private LayoutInflater mInflater;
    private boolean hasSIMCard;

    public InterfacePropleAdapter_ct(Context context, List<InterfacePeopleVO_ct> data) {
        if (data == null) {
            this.data = new ArrayList<InterfacePeopleVO_ct>();
        } else {
            this.data = data;
        }
        this.context = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        hasSIMCard = new CSIMCardUtil_ct(context).isHasSIMCard();
    }

    public void clear() {
        if (data != null) {
            this.data.clear();
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (data != null)
            return data.size();
        else
            return 0;
    }

    @Override
    public InterfacePeopleVO_ct getItem(int i) {
        // if(data != null && data.size() > 0 && i<data.size()){
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = mInflater.inflate(R.layout.item_interface_people_ct, null);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.colorShow = (TextView) view.findViewById(R.id.point_quan);
        holder.name = (TextView) view.findViewById(R.id.people_name);
        holder.adpt = (TextView) view.findViewById(R.id.people_adpt);
        holder.phone = (ImageView) view.findViewById(R.id.phone);

        view.setTag(holder);

        if (data != null && data.size() > 0) {
            final InterfacePeopleVO_ct vo = data.get(position);

            if (position % 5 == 0) {
                holder.colorShow.setBackground(context.getResources().getDrawable(
                        R.drawable.shape_red_ct)); // 前面的图片
            } else if (position % 5 == 1) {
                holder.colorShow.setBackground(context.getResources().getDrawable(
                        R.drawable.shape_blue_normal_ct)); // 前面的图片
            } else if (position % 5 == 2) {
                holder.colorShow.setBackground(context.getResources().getDrawable(
                        R.drawable.shape_00_ct)); // 前面的图片
            } else if (position % 5 == 3) {
                holder.colorShow.setBackground(context.getResources().getDrawable(
                        R.drawable.shape_1l_ct)); // 前面的图片
            } else if (position % 5 == 4) {
                holder.colorShow.setBackground(context.getResources().getDrawable(
                        R.drawable.shape_black_0f_ct)); // 前面的图片
            }

            String name = "";
            if (null != data.get(position)
                    && data.get(position).getName().length() > 2) {
                name = data.get(position).getName().substring(data.get(position).getName().length() - 2);
            } else {
                name = data.get(position).getName();
            }
            holder.colorShow.setText(name);
            holder.name.setText(this.data.get(position).getName());
            holder.adpt.setText(this.data.get(position).getAdpt());

            holder.phone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    //拨打电话
                    final MaterialDialog dialog = new MaterialDialog(context);
                    dialog.setTitle("拨打确认");
                    dialog.setMessage(data.get(position).getName() + "\n" +
                            data.get(position).getTel());
                    dialog.setPositiveButton("确认", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                            if(hasSIMCard){
                                Intent intent = new Intent().setData(Uri.parse("tel:" + data.get(position).getTel()));
                                intent.setAction(Intent.ACTION_CALL);
                                context.startActivity(intent);
                            }else{
                                CommonUtil.showToast(context,"SIM卡未插入或暂时不可用!");
                            }
                        }
                    });
                    dialog.setNegativeButton("取消", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();

                    /*if(hasSIMCard){
                        //拨打电话
                        Intent intent = new Intent().setData(Uri.parse("tel:" + data.get(position).getTel()));
                        intent.setAction(Intent.ACTION_DIAL);
                        context.startActivity(intent);
                    }else{
                        CommonUtil.showToast(context,"SIM卡未插入或暂时不可用!");
                    }*/
                }
            });
            //dialog显示接口人信息
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //拨打电话
                    final MaterialDialog dialog = new MaterialDialog(context);
                    dialog.setTitle("拨打确认");
                    dialog.setMessage(data.get(position).getName() + "\n" +
                            data.get(position).getTel());
                    dialog.setPositiveButton("确认", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                            if(hasSIMCard){
                                Intent intent = new Intent().setData(Uri.parse("tel:" + data.get(position).getTel()));
                                intent.setAction(Intent.ACTION_CALL);
                                context.startActivity(intent);
                            }else{
                                CommonUtil.showToast(context,"SIM卡未插入或暂时不可用!");
                            }
                        }
                    });
                    dialog.setNegativeButton("取消", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                }
            });
        }

        return view;
    }

    public void append(List<InterfacePeopleVO_ct> data) {
        if (data != null) {
            this.data.addAll(data);
            notifyDataSetChanged();
        }
    }

    class ViewHolder {
        TextView colorShow;
        TextView name;
        TextView adpt;
        ImageView phone;
    }

    //                  Intent intent = new Intent().setData(Uri.parse("tel:" + data.get(position).getTel()));
//                  intent.setAction(Intent.ACTION_CALL);
//                  context.startActivity(intent);
//                    final AlertDialog.Builder dialog = new AlertDialog.Builder(context,R.style.BaseDialog);
//                    dialog.setTitle("确认拨打");
//                    dialog.setMessage(data.get(position).getName() + "\n" +
//                            data.get(position).getTel());
//                    dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialogInterface, int i) {
//                            dialogInterface.dismiss();
//                        }
//                    });
//                    dialog.setPositiveButton("拨打", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialogInterface, int i) {
//                            dialogInterface.dismiss();
//                            Intent intent = new Intent().setData(Uri.parse("tel:" + data.get(position).getTel()));
//                            intent.setAction(Intent.ACTION_CALL);
//                            context.startActivity(intent);
//                        }
//                    });
//                    dialog.create().show();

}
